import 'package:flutter/material.dart';

class FormPage extends StatefulWidget {
  FormPage({Key key}) : super(key: key);

  @override
  _FormPageState createState() => _FormPageState();
}

class _FormPageState extends State<FormPage> {

  String username;
  int sex = 1;
  List hobby = [
    {
      'checked': true,
      'title': '吃　饭'
    },
    {
      'checked': false,
      'title': '睡　觉'
    },
    {
      'checked': true,
      'title': '打豆豆'
    },
  ];
  String info;

  List<Widget> _getHobby() {
    List<Widget> tempList = []; 
    for (var i = 0; i < this.hobby.length; i++) {
      tempList.add(
        Row(
          children: <Widget>[
            Text(this.hobby[i]['title'] + ':'),
            Checkbox(
              value: this.hobby[i]['checked'],
              onChanged: (value) {
                setState(() {
                  this.hobby[i]['checked'] = value;
                });
              }
            )
          ], 
        )
        
      );
    }
    return tempList;
  }

  void _sexChanged(val) {
    setState(() {
      this.sex = val;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('学生管理系统'), 
      ),
      body: Padding(
        padding: EdgeInsets.all(20),
        child: Column(
          children: <Widget>[
            TextField(
              decoration: InputDecoration(
                hintText: '请输入用户信息'
              ),
              onChanged: (val){
                setState(() {
                  this.username = val;
                });
              },
            ),
            SizedBox(height: 20),
            Row(
              children: <Widget>[
                Text('男'),
                Radio(value: 1, groupValue: this.sex, onChanged: this._sexChanged),
                SizedBox(width: 20),
                Text('女'),
                Radio(value: 2, groupValue: this.sex, onChanged: this._sexChanged)
              ],
            ),
            SizedBox(height: 20),
            // 爱好
            Column(
              children: this._getHobby(),
            ),
            SizedBox(height: 20),
            TextField(
              maxLines: 4,
              decoration: InputDecoration(
                hintText: '描述信息',
                border: OutlineInputBorder()
              ),
              onChanged: (value){
                setState(() {
                  this.info = value;
                });
              },
            ),


            SizedBox(height: 40), 
            Container(
              width: double.infinity,
              height: 40,
              child: RaisedButton(
                child: Text('提交信息'),
                onPressed: () {
                  print(this.sex);
                  print(this.username);
                  print(this.info);
                  print(this.hobby);
                },
                color: Colors.blue,
                textColor: Colors.white, 
              ),

            )
          ],
        ),
      )
    );
  }
}